package com.test.core.util;



import java.io.FileReader;
import java.io.FileWriter;
import java.io.RandomAccessFile;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;

public class FileUtil {
	/**
	 * 创建文件
	 * @author fengbo
	 * @param filePath
	 * @return
	 */
	public static synchronized java.io.File createFile(String filePath){
		try{
			java.io.File f = new java.io.File(filePath);
			if(!f.exists()){
				f.createNewFile();
			}
			return f;
		}catch(Exception e){
			return null;
		}
	}
	/**
	 * 创建目录
	 * @param filePath
	 * @return
	 */
	public static synchronized boolean createMkdir(String mkdirPath){
		try{
			java.io.File f = new java.io.File(mkdirPath);
			if(!f.exists()){
				return f.mkdirs();
			}
			return true;
		}catch(Exception e){
			return false;
		}
	}
	/**
	 * 删除文件
	 * @author fengbo
	 * @param filePath
	 * @return
	 */
	public static synchronized int delFile(String filePath){
		try{
			java.io.File f = new java.io.File(filePath);
			if(f.exists()){
				f.delete();
				return 1;
			}else{
				return 1;
			}
		}catch(Exception e){
			return 0;
		}
	}
	/**
	 * 获得文件中的内容
	 * @param filePath
	 * @return
	 */
	public static String  readFile(java.io.File file){
		StringBuffer sb = new StringBuffer();
		String str = "";
		try {
		FileReader fr = new java.io.FileReader(file);
			java.io.BufferedReader red = new java.io.BufferedReader(fr);
			while((str = red.readLine())!=null){
				sb.append(str+"\n");
			}
			red.close();
			fr.close();
		
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return sb.toString();
	}
	/**
	 * 写入文件
	 * @param file
	 * @param writeStr
	 * @return
	 * @modify by wangzc . date:2010-4-13 add return values;
	 */
	public static synchronized int writeFile(java.io.File file,String writeStr){
		try {
			java.io.FileOutputStream outStream = new java.io.FileOutputStream(file);
			outStream.write(writeStr.getBytes());
			outStream.flush();
			outStream.close();
			return 1;
		} catch (Exception e) {
			e.printStackTrace();
			return 0;
		}
		
	}
	/**
	 * 以追加方式写入文件
	 * @param file
	 * @param writeStr
	 * @return
	 */
	public static synchronized int writeFileAdd(java.io.File file,String writeStr){
		try {
			 FileWriter fw = new  FileWriter(file,true);
			 fw.write(writeStr);
			 fw.flush();
			 fw.close();
			//java.io.FileOutputStream outStream = new java.io.FileOutputStream(file);
			//outStream.write(writeStr.getBytes());
		} catch (Exception e) {
		
			e.printStackTrace();
		}
		return 0;
	}
	/**
	 * 给文件加锁
	 * @author fengbo
	 * @param file
	 * @return
	 */
	public static FileLock lockFie(java.io.File file){
		try {
		 RandomAccessFile raf = new RandomAccessFile(file, "rw"); //r ,rw ,rwd 三个可选参数
		 FileChannel fc = raf.getChannel();
		 FileLock fl = fc.tryLock();
		 return fl;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}

	}
	/**
	 * 释放文件锁
	 * @param fl
	 */
	public static void freeLock(FileLock fl){
		try{
			if(null!=fl){
				fl.release();
			}
		}catch(Exception e){
			
		}
	}
}
